﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Data;
using DBUtility;
using System.Web;

namespace CUREDataAccessLayers.Common
{
    public class common
    {
        private EventLogDal logdal = new EventLogDal();


        /// <summary>
        /// MD5加密方法
        /// </summary>
        /// <param name="txt"></param>
        /// <returns></returns>
        public  string GetStringMd5(string txt)
        {   
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string result = "";
            byte[] bytes = Encoding.ASCII.GetBytes(txt);
            byte[] cryptBytes = md5.ComputeHash(bytes);
            foreach (byte item in cryptBytes)
            {
                result += item.ToString("X2");
            }
            return result;
        }

        /// <summary>
        /// 返回最大ID+1
        /// </summary>
        /// <param name="columnname"></param>
        /// <param name="tablename"></param>
        /// <returns></returns>
        //public decimal GetMaxId(string columnname, string tablename)
        //{
        //    StringBuilder sql = new StringBuilder();
        //    sql.Append("select max(to_number(" + columnname + "))+1 from " + tablename);
        //    decimal num = 0;
        //    try
        //    {
        //        num = this.DbHelperInstance.ExecuteScalar<decimal>(sql);
        //    }
        //    catch (DbHelperException ex)
        //    {

        //        throw new DbHelperException(ex.Message, ex);
        //    }
        //    return num > 0 ? num : 1;
        //}

        /// <summary>
        /// 条件查询语句
        /// </summary>
        /// <param name="columnname"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public string SelectSqlStr(string columnname, object where)
        {
            string sql = "";
            if (where !=null && !string.IsNullOrEmpty(where.ToString()) && where.ToString() !="")
            {
                sql =" and "+ columnname + "='" + where.ToString() + "'";
            }
            return sql;
        }

        /// <summary>
        /// 判断是否存在重复数据
        /// </summary>
        /// <param name="columnname">列名</param>
        /// <param name="tablename">表名</param>
        /// <param name="columndata">数据</param>
        /// <param name="type">类型 1:整型 2:字符</param>
        /// <returns></returns>
        public StringBuilder JudgeExist(string columnname, string tablename, string columndata, int type)
        {
            StringBuilder sql = new StringBuilder();
            switch (type)
            {
                case 1: sql.Append("select " + columnname + " from " + tablename + " where " + columnname + " = " + columndata + "");
                break;
                case 2: sql.Append("select " + columnname + " from " + tablename + " where " + columnname + " = '" + columndata + "'");
                break; 
                default: break;
            }
            return sql;
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(string ColumnTable, int PageSize, int PageIndex, string strWhere, string filedOrder, out int Total,out int TotalCount)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSqlTotal = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" from(select rownum as rid, t.* from " + ColumnTable + " t where 1=1 ");
            strSqlTotal.Append(" select count(*) FROM " + ColumnTable + " t where 1=1 ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(strWhere);
                strSqlTotal.Append(strWhere);
            }
            strSql.Append(" and rownum<="+PageSize*PageIndex);

            if (filedOrder.Trim() != "")
            {
                strSql.Append(" order by " + filedOrder);
            }

            strSql.Append(") table_alias where table_alias.rid >"+(PageIndex-1)*PageSize);
            Total =Convert.ToInt32(DbHelperOra.Query(strSqlTotal.ToString()).Tables[0].Rows[0][0].ToString());
            TotalCount = (Total + PageSize - 1) / PageSize;
            return DbHelperOra.Query(strSql.ToString());
        }

        /// <summary>
        /// 插入异常(catch)日志
        /// </summary>
        /// <param name="msg"></param>
        public void InsertLog(string msg)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("msg", msg);
            dic.Add("masterid", HttpContext.Current.Request.Cookies["MWS_User"]["ID"]);
            logdal.InsertEventLog(dic);
        }

        /// <summary>
        /// 判断开关是否开启
        /// </summary>
        /// <param name="switchtype"></param>
        /// <returns></returns>
        public bool SwitchIsOn(string switchtype)
        {
            bool result = false;
            StringBuilder sql = new StringBuilder();
            sql.Append("select SWITCHVALUE from SYS_CONFIG where SWITCHCODE='" + switchtype + "'");
            try
            {
                object obj = DbHelperOra.GetSingle(sql.ToString());
                if (obj != null)
                {
                    int value = Convert.ToInt32(obj.ToString());
                    if (value == 1)
                    {
                        result = true;
                    }
                }
            }
            catch (Exception ex)
            {
                InsertLog(ex.Message);
            }
            return result;

        }

    }
}
